package com.zm.demo.disruptor.log;

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.EventHandlerGroup;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadFactory;

/**
 * @author z01140
 * @Package: com.zm.demo.disruptor.log
 * @Description:
 * @CreateDate 2023/8/29 17:51
 * @UpdateDate 2023/8/29 17:51
 */
public class Main {

    public static void main(String[] args) {
        int bufferSize = 1024*4;
        Disruptor<LogEvent> disruptor = new Disruptor<>(
                new LogEventFactory(), bufferSize, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r);
            }
        }, ProducerType.SINGLE, new YieldingWaitStrategy());

        disruptor.handleEventsWith(new LogEventHandler(), new LogEventHandler1());
        disruptor.start();
        RingBuffer<LogEvent> ringBuffer = disruptor.getRingBuffer();
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long i = 0; i < 100; i++) {
            bb.putLong(0,i);
            System.out.println("生产：" + Thread.currentThread().getName() + " " + i);
            ringBuffer.publishEvent((event, sequence, buffer) -> event.setValue(buffer.getLong(0)), bb);
        }

        disruptor.shutdown();
    }
}
